package cn.inno.login;

import cn.inno.entitys.ErrorMsg;
import cn.inno.exceptions.BusinessException;
import cn.inno.service.BaseService;
import net.sf.json.JSONObject;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 管理后台服务类
 *
 * @author LeungZengJian
 */
@Service
public class LoginService extends BaseService {

    @Autowired
    private LoginDao loginDao;

    public JSONObject get(String name, String password) {

        if (StringUtils.isBlank(name)) {
            throw new BusinessException(ErrorMsg.CALL_FAIL, "用户名不能为空");
        }
        if (StringUtils.isBlank(password)) {
            throw new BusinessException(ErrorMsg.CALL_FAIL, "登陆密码不能为空");
        }

        Map formMap = new HashMap();
        formMap.put("name", name);
        formMap.put("password", password);

        List lists = this.loginDao.get(formMap);

        if (lists.size() != 1) {
            throw new BusinessException(ErrorMsg.CALL_FAIL, "账号或密码错误，请重新输入");
        }

        this.setObject("user", lists.get(0));

        JSONObject jsonObject = new JSONObject();
        return this.executeRerutn(jsonObject);
    }

}
